<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="发票类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择报销类型">
          <el-option
            v-for="item in tabList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="报销时间" prop="bxsj">
        <el-input
          v-model="queryParams.bxsj"
          placeholder="请输入报销时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="报销类型" prop="bxlx">
        <el-select
          v-model="queryParams.bxlx"
          clearable
          placeholder="请选择报销类型"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发票代码" prop="fpdm">
        <el-input
          v-model="queryParams.fpdm"
          placeholder="请输入发票代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发票号码" prop="fphm">
        <el-input
          v-model="queryParams.fphm"
          placeholder="请输入发票号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报销人" prop="bxr">
        <el-select
          v-model="queryParams.bxr"
          clearable
          placeholder="请选择报报销人"
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="销方名称" prop="mc2">
        <el-input
          v-model="queryParams.mc2"
          placeholder="请输入销方名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          >导入</el-button
        >
      </el-col>
    </el-row>
    <el-table
      style="margin-top: 20px"
      v-loading="loading"
      :data="invoiceExpensesList"
      @selection-change="handleSelectionChange"
      @row-click="handleCurrentChange"
      ref="multipleTable"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column
        label="发票类型"
        align="center"
        prop="type"
        min-width="76"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.type === '01'">专用发票</span>
          <span v-if="scope.row.type === '02'">普通发票</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发票号码"
        align="center"
        prop="fphm"
        min-width="180"
      />
      <el-table-column
        label="发票代码"
        align="center"
        prop="fpdm"
        min-width="120"
      />
      <el-table-column label="销方名称" align="center" prop="mc2" />
      <el-table-column label="开票金额" align="center" prop="hjje" />
      <el-table-column
        label="开票日期"
        align="center"
        prop="kprq"
        min-width="90"
      />
      <el-table-column
        label="报销日期"
        align="center"
        prop="createTime"
        min-width="150"
      >
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column label="报销人" align="center" prop="bxr" />
      <el-table-column label="报销类型" align="center" prop="typeName" />

      <el-table-column
        label="归档状态"
        align="center"
        prop="catalogueStatus"
        min-width="75"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.catalogueStatus === '0'">未归档</span>
          <span v-if="scope.row.catalogueStatus === '1'">已归档</span>
        </template>
      </el-table-column>
      <el-table-column label="凭证号" align="center" prop="pzh" width="160">
        <template slot-scope="scope">
          <div>
            <el-input
              size="small"
              v-model="scope.row.pzh"
              placeholder="请输入凭证号"
              @blur="inputBlur(scope.row)"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        fixed="right"
        width="190"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            style="width: 80px"
            v-if="scope.row.catalogueStatus === '1'"
            size="mini"
            type="primary"
            @click="handleAdd(scope.row)"
            >查看发票</el-button
          >
          <el-button
            style="width: 80px"
            v-if="scope.row.catalogueStatus === '0'"
            size="mini"
            type="primary"
            @click="goFile"
            >归档</el-button
          >
          <!-- <el-button size="mini" type="success" @click="goFile"
            >修改凭证号</el-button
          > -->
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 发票详情 -->
    <el-dialog :title="title" fullscreen :visible.sync="open" append-to-body>
      <div>
        <iframe
          :src="iframeUrl"
          style="width: 100%; height: calc(100vh - 130px)"
        ></iframe>
      </div>
    </el-dialog>
    <!-- 添加或修改发票报销对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="发票类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入发票类型" />
        </el-form-item>
        <el-form-item label="发票号码" prop="fphm">
          <el-input v-model="form.fphm" placeholder="请输入发票号码" />
        </el-form-item>
        <el-form-item label="发票号码" prop="fpdm">
          <el-input v-model="form.fpdm" placeholder="请输入发票号码" />
        </el-form-item>
        <el-form-item label="开票金额" prop="hjje">
          <el-input v-model="form.hjje" placeholder="请输入开票金额" />
        </el-form-item>
        <el-form-item label="开票日期" prop="kprq">
          <el-input v-model="form.kprq" placeholder="请输入开票日期" />
        </el-form-item>
        <el-form-item label="报销日期" prop="bxsj">
          <el-input v-model="form.bxsj" placeholder="请输入报销日期" />
        </el-form-item>
        <el-form-item label="报销人" prop="bxr">
          <el-input v-model="form.bxr" placeholder="请输入报销人" />
        </el-form-item>
        <el-form-item label="报销类型" prop="bxlx">
          <el-input v-model="form.bxlx" placeholder="请输入报销类型" />
        </el-form-item>

        <el-form-item label="销方名称" prop="mc2">
          <el-input v-model="form.mc2" placeholder="请输入销方名称" />
        </el-form-item>
        >
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="
          upload.url +
          '?updateSupport=' +
          upload.updateSupport +
          '&deptId=' +
          selectId
        "
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <!-- <div class="el-upload__tip" slot="tip">
              <el-checkbox v-model="upload.updateSupport" />
              是否更新已经存在的用户数据
            </div> -->
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link
            type="primary"
            :underline="false"
            style="font-size: 12px; vertical-align: baseline"
            @click="importTemplate"
            >下载模板</el-link
          >
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInvoiceExpenses,
  getInvoiceExpenses,
  delInvoiceExpenses,
  addInvoiceExpenses,
  updateInvoiceExpenses,
} from "@/api/business/invoiceExpenses";
import { listAllInvoiceExpensesType } from "@/api/business/invoiceExpensesType";
import { listAllInvoiceExpensesUser } from "@/api/business/invoiceExpensesUser";
import { getToken } from "@/utils/auth";

export default {
  name: "InvoiceExpenses",
  data() {
    return {
      iframeUrl: "",
      tabList: [
        { name: "专用发票", value: "01" },
        { name: "普通发票", value: "02" },
      ],
      dateRange: [],
      userList: [],
      typeList: [],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票报销表格数据
      invoiceExpensesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API + "/business/invoiceExpenses/importData",
      },
      selectId: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        bxr: null,
        bxsj: null,
        bxlx: null,
        fpdm: null,
        fphm: null,
        hjje: null,
        type: null,
        kprq: null,
        hjse: null,
        mc2: null,
        remark: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bxr: [{ required: true, message: "报销人不能为空", trigger: "blur" }],
        bxsj: [
          { required: true, message: "报销时间不能为空", trigger: "blur" },
        ],
        bxlx: [
          { required: true, message: "报销类型不能为空", trigger: "blur" },
        ],
        fpdm: [
          { required: true, message: "发票代码不能为空", trigger: "blur" },
        ],
        fphm: [
          { required: true, message: "发票号码不能为空", trigger: "blur" },
        ],
        hjje: [
          { required: true, message: "合计金额不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "发票类型不能为空", trigger: "change" },
        ],
        kprq: [
          { required: true, message: "开票日期不能为空", trigger: "blur" },
        ],
        hjse: [
          { required: true, message: "合计税额不能为空", trigger: "blur" },
        ],
        mc2: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.selectId = JSON.parse(localStorage.getItem("selectCompany")).deptId;
    this.queryParams.deptId = this.selectId;
    this.getList();
    this.getUser();
    this.getType();
  },
  methods: {
    inputBlur(row) {
      this.form.id = row.id;
      this.form.pzh = row.pzh;
      updateInvoiceExpenses(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
    goFile() {
      this.$router.push("file");
    },
    /** 查询发票报销列表 */
    getList() {
      this.loading = true;
      listInvoiceExpenses(this.queryParams).then((response) => {
        this.invoiceExpensesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 报销人 */
    getUser() {
      listAllInvoiceExpensesUser({ parentDeptId: this.selectId }).then(
        (response) => {
          this.userList = response.data;
        }
      );
    },
    //类型
    getType() {
      listAllInvoiceExpensesType({ deptId: this.selectId }).then((response) => {
        this.typeList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        pzh: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleCurrentChange(val) {
      this.$refs.multipleTable.toggleRowSelection(val);
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.iframeUrl = "";
      this.iframeUrl = "https://yp.cf37.cn" + row.fileUrl;
      console.log(this.iframeUrl);
      this.open = true;
      this.title = "查看发票";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getInvoiceExpenses(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改发票报销";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInvoiceExpenses(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInvoiceExpenses(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该发票记录？")
        .then(function () {
          return delInvoiceExpenses(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/invoiceExpenses/export",
        {
          ...this.queryParams,
        },
        `invoiceExpenses_${new Date().getTime()}.xlsx`
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/invoice/export",
        {
          ...this.queryParams,
        },
        `报销记录_${new Date().getTime()}.xlsx`
      );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "报销记录导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download(
        "business/invoiceExpenses/importTemplate",
        {},
        `报销记录_${new Date().getTime()}.xlsx`
      );
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
  },
};
</script>
<style>
.app-container {
  padding: 20px 30px;
}
</style>
